<?php
/*------------------------------------------------------------------------------
this file is part of Web Application Kit http://webappkit.net
Copyright (C) 2006 J.Ducastel <fredbird@fredbird.org>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
------------------------------------------------------------------------------*/

/**
* webappkit data routing service
* @author J.Ducastel <fredbird@fredbird.org>
* @version 
*/
class events_dataRouter extends wakService {
/*------------------------------------------------------------------------------
                                                                     constructor
------------------------------------------------------------------------------*/
	/**
	*
	* @access
	* /
	function wakDataRouter() {
		$this->__construct();
	}/**/
	
	/**
	*
	* @access
	*/
	public function __construct() {
		parent::__construct();
		//$this->debug=&wak::getservice('wakdebug');
		//$this->lib=&wak::getservice('waklib');
		//$this->lib->needClass('httpRequestBean');
		//$this->lib->needClass('datapath');
		$this->request=&httpRequestBean::getCurrent();
		// avoiding magic quotes
		$this->_remove_magic_quotes();
	}/**/
/*------------------------------------------------------------------------------
                                                                  public methods
------------------------------------------------------------------------------*/
	/**
	* Retrieves data from data source
	* @param string $route
	* @access public
	* @return mixed
	*/
	function getDataFrom($route) {
		// parsing route
		if (!$parsed=$this->_parseRoute($route)) {
			// $this->debug->warning("invalid route {$route}");
			return null;
		}
		// getting appropriate method
		$method='_getFromDomain_'.$parsed['domain'];
		//echo $method;
		if (method_exists($this,$method)) {
			return $this->$method($parsed['path']);
		}
		// failed
		$this->debug->warning("failed getting data from {$route}");
		return null;
	}/**/
	
	/**
	* references/passes data to route
	* @param & $value value to forward
	* @param string $route
	* @access public
	* @return bool
	*/
	function sendDataTo(& $data, $route) {
		// parsing route
		if (!$parsed=$this->_parseRoute($route))
			return false;
		// echo "<p>routing data to $route</p>";
		// searching for appropriate method
		$method='_sendToDomain_'.$parsed['domain'];
		if (method_exists($this,$method)) {
			return $this->$method($data,$parsed['path']);
		}
		// failed
		$this->debug->warning("failed to send data to $route");
		return false;
	}/**/
	
	/**
	* 
	* @param
	* @access public
	* @return 
	* /
	function () {
		
	}/**/
	
	/**
	* 
	* @param
	* @access public
	* @return 
	* /
	function () {
		
	}/**/
/*------------------------------------------------------------------------------
                                                                      properties
------------------------------------------------------------------------------*/
	/**
	* @var type desc
	*/
	var $request; // current wakHttpRequest instance
/*------------------------------------------------------------------------------
                                                                 private methods
------------------------------------------------------------------------------*/
	/**
	* first route parsing
	* @param string $route
	* @access private
	* @return array 
	*/
	function _parseRoute($route) {
		if (!ereg('^([a-z]+):(.*)$',$route,$parts)) {
			$this->debug->notice("failed to parse route $route");
			return false;
		}
		return array(
			'domain'=>$parts[1]
			, 'path'=>$parts[2]?$parts[2]:null
		);
	}/**/
	
	/**
	* sends data to global domain
	* @param mixed &$data
	* @param string $path
	* @access private
	* @return bool
	*/
	function _sendToDomain_global(&$data, $path) {
		return datapath::set($GLOBALS,$data,$path);
		//$GLOBALS[$parsed['path']]=&$data;
		return true;
	}/**/
	
	/**
	* adds content
	* @param mixed &$data
	* @param string $path (set)|(insert).{index}
	* @access private
	* @return bool
	*/
	function _sendToDomain_addContent(&$data, $path) {
		// getting response builder
		$builder=&wak::getservice('wakresponsebuilder');
		return $builder->addContent($data,$path);
		//return false;
	}/**/
	
	/**
	* 
	* @param mixed &$data
	* @param string $path
	* @access private
	* @return bool
	*/
	function _sendToDomain_setContent(&$data, $path) {
		// getting response builder
		$builder=&wak::getservice('wakresponsebuilder');
		return $builder->setContent($data,$path);
	}/**/
	
	/**
	* 
	* @param mixed &$data
	* @param string $path
	* @access private
	* @return bool
	* /
	function _sendToDomain_(&$data, $path) {
		
	}/**/
	
	/**
	* 
	* @param mixed &$data
	* @param string $path
	* @access private
	* @return bool
	* /
	function _sendToDomain_(&$data, $path) {
		
	}/**/
	
	/**
	* Return uri segment part
	* @param string $path
	* @access private
	* @return string
	*/
	function _getFromDomain_uriSegment($path) {
		//echo $path;
		return $this->request->getUriSegment($path);
	}/**/
	
	/**
	* get some global var
	* @param string $path
	* @access private
	* @return mixed
	*/
	function _getFromDomain_global($path) {
		return datapath::get($GLOBALS,$path);
	}/**/
	
	/**
	* get var from post
	* @param
	* @access private
	* @return 
	*/
	function _getFromDomain_post($path) {
		return datapath::get($_POST,$path);
	}/**/
	
	/**
	* get var from get data
	* @param
	* @access private
	* @return 
	*/
	function _getFromDomain_get($path) {
		return datapath::get($_GET,$path);
	}/**/
	
	/**
	* get var from get data
	* @param
	* @access private
	* @return 
	*/
	function _getFromDomain_cookie($path) {
		return datapath::get($_COOKIE,$path);
	}/**/
	
	/**
	* get from current date
	* @param
	* @access private
	* @return 
	*/
	function _getFromDomain_date($path) {
		return date($path);
	}/**/
	
	/**
	* 
	* @param
	* @access private
	* @return 
	* /
	function _getFromDomain_() {
		
	}/**/
	
	/**
	* 
	* @param
	* @access private
	* @return 
	* /
	function _getFromDomain_() {
		
	}/**/

	
	/**
	* 
	* @param
	* @access private
	* @return 
	*/
	function _remove_magic_quotes() {
		if (get_magic_quotes_gpc()==1) {
			foreach (array_keys($_POST) as $key)
				$_POST[$key]=stripslashes($_POST[$key]);
		}
		return true;
	}/**/
	
	/**
	* 
	* @param
	* @access private
	* @return 
	* /
	function () {
		
	}/**/
}
?>
